package com.jrh.web.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jrh.dal.LabelDao;
import com.jrh.domain.entity.Label;
import com.jrh.web.admin.form.AdminFormCopier;
import com.jrh.web.admin.form.AdminLabelForm;

/**
 * 风格管理
 * 
 * @author jasen.hsu.cn@gmail.com 2012-8-10 下午7:45:31
 */
@Controller
@RequestMapping("/admin")
public class AdminStyleController {

    private static final String LABEL_TYPE = Label.LABEL_STYLE_KEY;

    @Autowired
    private LabelDao            labelDao;

    // ====================style==================================================================
    @RequestMapping("/style")
    public String listStyle(Model model) {
        List<Label> labels = labelDao.listChildLabel(LABEL_TYPE);
        model.addAttribute("_data_", labels);
        return "admin/style/list";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/style/create")
    public String doCreate(@ModelAttribute("labelForm") AdminLabelForm labelForm) {
        labelForm.setParent(LABEL_TYPE);
        Label label = AdminFormCopier.convert(labelForm, null);
        labelDao.create(label);
        return "redirect:/admin/style.htm";
    }

    @RequestMapping("/style/{id}")
    public String editStyle(@PathVariable Long id, Model model) {
        Label label = labelDao.load(id);
        model.addAttribute("_data_", label);
        return "admin/style/edit";
    }

    @RequestMapping("/style/edit")
    public String doEditStyle(@ModelAttribute("labelForm") AdminLabelForm labelForm, Model model) {
        labelForm.setParent(LABEL_TYPE);
        Label label = AdminFormCopier.convert(labelForm, null);
        labelDao.update(label);
        return "redirect:/admin/style.htm";
    }

    @RequestMapping("/style/delete/{id}")
    public String deleteStyle(@PathVariable Long id, Model model) {
        labelDao.delete(id);
        return "redirect:/admin/style.htm";
    }
}
